

namespace Stee.CAP8.MessageBus.Kafka.Models
{
    using System;
    using NLog;
    using Stee.CAP8.MessageBus.Interfaces.Models;

    /// <summary>
    /// Handler for generic message type
    /// </summary>
    internal sealed class HandlerGenericDetails : HandlerDetails<GenericMessage>
    {
        private readonly static Logger Logger = LogManager.GetCurrentClassLogger();


        #region Fields

        /// <summary>
        /// Overridden logger
        /// </summary>
        protected override Logger NLogger => Logger;

        #endregion Fields


        #region Constructor

        /// <summary>
        /// Initializes a new instance of the HandlerGenericDetails class
        /// </summary>
        internal HandlerGenericDetails() { }

        #endregion Constructor


        #region Public Methods

        /// <summary>
        /// Invoke message handling
        /// </summary>
        /// <param name="topicName">Message topic name</param>
        /// <param name="message">Generic message</param>
        /// <param name="publishUtcDT">Message published date time in UTC</param>
        /// <returns>Return TRUE if invoked successfully else FALSE</returns>
        internal override bool Invoke(TopicName topicName, GenericMessage message, DateTime publishUtcDT)
        {
            try
            {
                if (topicName == null)
                {
                    throw new ArgumentNullException($"Failed to invoke event message handling, parameter \"{nameof(topicName)}\" is null.");
                }
                else if (message == null)
                {
                    throw new ArgumentNullException($"Failed to invoke event message handling, parameter \"{nameof(message)}\" is null.");
                }

                //// Topic
                var topic = topicName.Name;

                if (this.Handlers.ContainsKey(topic))
                {
                    var topicHandlers = this.Handlers[topic];
                    topicHandlers?.Invoke(topicName, message, publishUtcDT);
                }

                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, ex.Message);
                return false;
            }
        }

        #endregion Public Methods
    }
}
